#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    // 设置按钮的图标
    QIcon icon(":/cute.png");
    ui->pushButton->setIcon(icon);
    ui->pushButton->setIconSize(QSize(80, 80));

    // 设置热键 (默认连发)
    // 直接通过按键名字设置
//    ui->pushButton_up->setShortcut(QKeySequence("w"));
//    ui->pushButton_down->setShortcut(QKeySequence("s"));
//    ui->pushButton_left->setShortcut(QKeySequence("a"));
//    ui->pushButton_right->setShortcut(QKeySequence("d"));

    // 通过枚举设置
    // ui->pushButton_up->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_W)); // 组合键
    ui->pushButton_up->setShortcut(QKeySequence(Qt::Key_W));
    ui->pushButton_down->setShortcut(QKeySequence(Qt::Key_S));
    ui->pushButton_left->setShortcut(QKeySequence(Qt::Key_A));
    ui->pushButton_right->setShortcut(QKeySequence(Qt::Key_D));
}

Widget::~Widget()
{
    delete ui;
}


void Widget::on_pushButton_up_clicked()
{
    QRect rect = ui->pushButton->geometry();
    ui->pushButton->move(rect.x(), rect.y() - 5);
}

void Widget::on_pushButton_down_clicked()
{
    QRect rect = ui->pushButton->geometry();
    ui->pushButton->move(rect.x(), rect.y() + 5);
}

void Widget::on_pushButton_left_clicked()
{
    QRect rect = ui->pushButton->geometry();
    ui->pushButton->move(rect.x() - 5, rect.y());
}

void Widget::on_pushButton_right_clicked()
{
    QRect rect = ui->pushButton->geometry();
    ui->pushButton->move(rect.x() + 5, rect.y());
}
